﻿using UnityEngine;
using System;
using Object = UnityEngine.Object;

namespace FrameWorkSong
{
    public class ResourcesRes : Res
    {
        private string my_Path;
        public ResourcesRes(string name,string path)
        {
            my_Path = path.Substring("resources://".Length);
            State = ResState.Waiting;
            Name = name;
        }
        public override bool LoadAssetSync()
        {
            State = ResState.Loading;
            Asset = Resources.Load(my_Path);
            State = ResState.Loaded;
            return Asset;
        }

        public override void LoadAssetAsync()
        {
            State = ResState.Loading;
            var resRequest = Resources.LoadAsync(my_Path);
            resRequest.completed += operation =>
            {
               // Debug.Log(operation.progress);
                Asset = resRequest.asset;
                State = ResState.Loaded;
            };
        }
        protected override void OnReleaseRes()
        {
            ReleaseAsset();
            ResMgr.Instance.ShareLoadedAssets.Remove(this);
           
        }


      
        /// <summary>
        /// 销毁方法
        /// </summary>
        public override void ReleaseAsset()
        {
            if (Asset is GameObject)
            {
                Resources.UnloadUnusedAssets();
            }
            else
            {
                Resources.UnloadAsset(Asset);
            }
            Asset = null;
        }
    }
}
